package com.testing.sunnycommon.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.testing.sunnycommon.annotations.SunnyAdvice;
import com.testing.sunnycommon.model.Result;
import com.testing.sunnycommon.model.SunnyVariable;
import com.testing.sunnycommon.service.SunnyProjectService;
import com.testing.sunnycommon.service.SunnyVariableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname SunnyVariableController
 * @Description 类型说明
 * @Date 2023/5/28 20:08
 * @Created by 特斯汀Roy
 */
@RequestMapping("/sunnyVariable")
@RestController
public class SunnyVariableController {

    @Autowired
    SunnyVariableService sunnyVariableService;

    @Autowired
    SunnyProjectService sunnyProjectService;

    @GetMapping("/{projectId}")
    public Result<List<SunnyVariable>> listVariablesInProject(@PathVariable String projectId) {
        List<SunnyVariable> variableList = sunnyVariableService.list(
                new LambdaQueryWrapper<SunnyVariable>()
                        .eq(SunnyVariable::getProjectId, projectId));

        return Result.success("查询项目" + projectId + "的变量成功", variableList);
    }


    /**
     * add和update方法，其实可以合并到post方法里，这里单纯为了更好地符合规范拆分的。
     *
     * @param sunnyVariable
     * @return
     */
    @SunnyAdvice("新增项目变量")
    @PostMapping
    public Result addVariable(@RequestBody SunnyVariable sunnyVariable) {
        boolean addResult = sunnyVariableService.saveOrUpdateVariable(sunnyVariable);
        if (addResult) {
            return Result.success("在项目【" + sunnyProjectService.getById(sunnyVariable.getProjectId()).getName()
                    + "】中新增变量：" + sunnyVariable.getVarName()
                    + "=" + sunnyVariable.getVarValue(), sunnyVariable);
        } else {
            return Result.fail(1010, "新增变量失败", sunnyVariable);
        }
    }

    @SunnyAdvice("修改项目变量")
    @PutMapping
    public Result updateVariable(@RequestBody SunnyVariable sunnyVariable) {
        boolean updateResult = sunnyVariableService.saveOrUpdateVariable(sunnyVariable);
        if (updateResult) {
            return Result.success("修改项目【"
                    + sunnyProjectService.getById(sunnyVariable.getProjectId()).getName()
                    + "】中的变量" + sunnyVariable.getVarName() + "的值为"
                    + sunnyVariable.getVarValue(), sunnyVariable);
        } else {
            return Result.fail(1020, "修改项目【"
                    + sunnyProjectService.getById(sunnyVariable.getProjectId()).getName()
                    + "】中的变量" + sunnyVariable.getVarName() + "失败", sunnyVariable);
        }
    }

    /**
     * 前端其实没有提供批量删除，所以这个批量删除功能，其实是用来测试后端的。
     *
     * @param ids
     * @return
     */
    @SunnyAdvice("删除项目变量")
    @DeleteMapping("/{ids}")
    public Result deleteVariables(@PathVariable List<Long> ids) {
        SunnyVariable deleteVariable = sunnyVariableService.getById(ids.get(0));
        boolean deleteResult = sunnyVariableService.removeBatchByIds(ids);
        if (deleteResult) {
            return Result.success("删除项目【"
                    + sunnyProjectService.getById(deleteVariable.getId()).getName()
                    + "】中的变量" + deleteVariable.getVarName() + "成功",deleteVariable);
        } else {
            return Result.fail(1020, "删除变量失败", "尝试删除的变量有:" + ids);
        }
    }


}
